<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/20
 * Time: 14:37
 */
namespace app\badminton\controller;

use app\badminton\model\Adminuser;
use think\Controller;
use think\Cookie;

class Login extends Controller{

    //用户登录
    public function login(){
        if(request()->isPost()){
            $data = request()->post();
            $user_info = Adminuser::where('username',$data['username'])->where('password',md5($data['password']))->find();
            if(empty($user_info)){
                return_json(0,'用户名或密码错误');
            }else{
                $time = time();
                $ip = request()->ip();
                Cookie::set('username',$user_info['username'],3600);
                try{
                    dolog($user_info['id'],$user_info['username'],'登入系统');
                    Adminuser::where('username',$user_info['username'])->update(['ip'=>$ip,'login_time'=>$time]);
                }catch (\Exception $e){
                    return_json(0,'系统错误');
                }
                return_json(1,'登录成功');
            }
        }
        return view();
    }

    //退出登录
    public function logout(){

        Cookie::delete('username');
        return redirect('admin/login/login');
    }
}